package cn.elegent.tx.core.sys;

import cn.elegent.tx.annotation.RollBack;
import cn.elegent.tx.core.rollback.RollBackHandler;
import cn.elegent.ac.util.SpringAopTargetUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * RollBackHandlerLoader
 * @description 回滚类加载器
 * @author WGL
 * @date 2022/11/7 19:21
*/
@Component
@Slf4j
public class RollBackHandlerLoader implements ApplicationContextAware {


    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        log.info("回滚处理类加载器启动");
        Map<String, RollBackHandler> map = ctx.getBeansOfType(RollBackHandler.class);//拿到IOC容器中所有的MsgHandler的类
        map.values().stream().forEach(v->{
            try {
                RollBack annotation = SpringAopTargetUtil.getTarget(v).getClass().getAnnotation(RollBack.class);  //获取注解
                if (annotation != null) {
                    RollBackData.rollBackMap.put(annotation.mainName()+"/"+annotation.childName(), v);  //处理器集合
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        });
    }
}